#include "public.h"

#pragma section (fcode)
void USART_Init ( USART_Type_Param1  Init_Value1, USART_Type_Param2  Init_Value2)
{   
   	uchar temp=0;
	USART_CR1 = USART_DEFAULT_PARAM1;                         /*Load Default value*/
	USART_CR2 = USART_DEFAULT_PARAM2;
	temp = USART_SR;                    /*Clear all the flags in Status register*/
	temp = USART_DR;
	USART_DR = USART_DEFAULT_PARAM1;
	USART_CR1 |= (unsigned char)Init_Value1;/*Sets SCI in the required mode*/
	USART_CR2 |= (unsigned char)Init_Value2;

}   
void USART_IT_Enable(USART_IT_Type USART_IT_Param)/*Enables the desired the interrupt */
{
      USART_CR2 |= (unsigned char)USART_IT_Param;   
}








void USART_PutByte (unsigned char Tx_DATA)
{  
    uchar temp= USART_SR;
    USART_DR = Tx_DATA;
    while (!(USART_IsTransmitCompleted()));  


}       








bool USART_IsTransmitCompleted(void)
{
    if(USART_SR & USART_TC)                  /*Checks if transmission is completed*/
    {
        return (1);
    }
    else
    {
        return(0);
    }
}
void USART_PutString(const unsigned char *PtrtoString)
{   
    uchar temp=0;
    uchar Val = *PtrtoString;
    while (Val != '\0')                        /*Checks for the end of string*/
    {
        Val= *PtrtoString;                    
        temp = USART_SR;
        USART_DR = *PtrtoString;        /*Copies the character to be transmitted*/
        while(!(USART_IsTransmitCompleted()));   

        PtrtoString++;               /*Moves the pointer to the next location*/
    }
    USART_CR2 |= (unsigned char)USART_TDRE;                     /*Enable Interrupt*/
} 









void USART_PutBuffer (const unsigned char *PtrtoBuffer, signed char Length)
{  
    uchar temp=0, i=0;
	for(i=0;i<Length;i++)
    {
         temp = USART_SR;
	     USART_DR = *PtrtoBuffer;              /*Loads the byte to be transmitted*/
      	 while (!(USART_IsTransmitCompleted()));  

        	PtrtoBuffer++;               /*Moves the pointer to the next location*/
    }
}      




void BT_UART_Rx_Data(int8u bRxData)
{    

	if(rx_index>= USART_RX_BUFF_SIZE)
    {
		rx_index = 0;
		return;
    }
 	if((rx_index > 0)&&(bRxData == CMD_END_2)&&(BT_DataRxBuf[rx_index-1] == CMD_END_1))
    {
		BT_DataRxBuf[rx_index] = CMD_END_2;
		BT_DataRxBuf[rx_index+1] = '\0';
		rx_index = 0;	
		BlueTooth_DataAnlyse();	
    }
    else
    {
		BT_DataRxBuf[rx_index] = bRxData;
		rx_index++;			
    }
	return;
}










void  ISR_USART (void)
{   
    uchar temp,bRxData;
    if (USART_SR & USART_RDRF)	//Received data successfully
    {
	   bRxData= USART_DR;	// input receive data
	   BT_UART_Rx_Data(bRxData); 
	   return;
    }
	if(USART_SR&USART_IDLE)
    {

		temp=USART_SR;
		temp=USART_DR;
		return;
    }
	if((USART_SR&USART_ORE)||(USART_SR&USART_NE)||(USART_SR&USART_FE)||(USART_SR&USART_PE))
    {


		temp=USART_SR;
		temp=USART_DR;
		USART_IT_Enable(USART_IDLE_LINE);
		return;
    }
     return;
}
#pragma section ()

